package com.controller;

import com.model._do.SignUpDO;
import com.model.dto.baseDTO.ResultDTO;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sign")
public class SignController {

    @Autowired
    private ResultService resultService;

    @Autowired
    private SignUpService signUpService;

    @Autowired
    private EquipmentService equipmentService;

    /**
     * 通过userId, equipId获取sign元组
     * @param sign userId，equipId
     * @return sign元组
     */
    @RequestMapping("/getSign")
    public ResultDTO getSign(@RequestBody SignUpDO sign){
        return resultService.createSuccess("success", signUpService.getSignedByUserEquip(sign.getUserId(), sign.getEquipId()));
    }

    /**
     * 添加新的sign元组，patientConfirm，doctorConfirm均为空
     * @param sign equipId，userId
     * @return 成功提示
     */
    @RequestMapping("/addNewSign")
    public ResultDTO addNewEquip(@RequestBody SignUpDO sign){
        if(StringUtils.isEmpty(sign.getEquipId()) || StringUtils.isEmpty(sign.getUserId())){
            return resultService.createFailed("设备或用户id为空");
        }
        try{
            String result = equipmentService.addNewEquip(sign);
            if(result.equals("success")){
                return resultService.createSuccess("success");
            } else {
                return resultService.createFailed(result);
            }
        } catch (Exception e){
            e.printStackTrace();
            return resultService.createFailed("数据请求异常");
        }
    }

    /**
     * 根据id，将sign表patientConfirm置为disagree
     * @param sign id
     * @return 成功标识
     */
    @RequestMapping(value = "/cancelAddNewSign", method = RequestMethod.POST)
    public ResultDTO cancelAddNewSign(@RequestBody SignUpDO sign){
        try{
            signUpService.cancelAddNewSign(sign);
            return resultService.createSuccess("success");
        } catch (Exception e){
            return resultService.createFailed("failed");
        }
    }

    /**
     * 根据userId，equipId将sign其中某个confirm置为disagree
     * @param sign  userId，equipId
     * @return 成功提示
     */
    @RequestMapping("/deleteSign")
    public ResultDTO patientDeleteSign(@RequestBody SignUpDO sign){
        try {
            signUpService.deleteSignUp(sign);
            return resultService.createSuccess("success");
        } catch (Exception e) {
            return resultService.createFailed("failed");
        }
    }

    /**
     * 根据userId,equipId将sign两个confirm置为agree
     * @param sign userId，equipId
     * @return 成功提示
     */
    @RequestMapping(value = "/doConfirm", method = RequestMethod.POST)
    public ResultDTO doConfirmSign(@RequestBody SignUpDO sign){
        boolean result = signUpService.doConfirmSign(sign);
        if(result){
            return resultService.createSuccess("success");
        } else {
            return resultService.createFailed("failed");
        }
    }
}
